<template>
  <div class="app-container">
    <el-tabs v-model="activeTab">
      <el-tab-pane label="报销申请" name="reimbursement">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="参保人" prop="insuredName">
            <el-input
              v-model="queryParams.insuredName"
              placeholder="请输入参保人姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="申请状态" clearable>
              <el-option label="待处理" :value="0" />
              <el-option label="已转账" :value="1" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="reimbursementList">
          <el-table-column label="序号" align="center" type="index" width="50" />
          <el-table-column label="参保人" align="center" prop="insuredName" />
          <el-table-column label="身份证号" align="center" prop="idCard" :show-overflow-tooltip="true" />
          <el-table-column label="医疗机构" align="center" prop="hospitalName" />
          <el-table-column label="批准时间" align="center" prop="approveTime" />
          <el-table-column label="报销金额" align="center" prop="reimbursementAmount" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <el-tag type="warning" v-if="scope.row.status === 0">待处理</el-tag>
              <el-tag type="success" v-else-if="scope.row.status === 1">已转账</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="handleTransfer(scope.row)"
                :disabled="scope.row.status === 1"
              >处理转账</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleDetail(scope.row)"
              >查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-tab-pane>
      
      <el-tab-pane label="转账记录" name="transfer">
        <el-form :model="transferQuery" ref="transferForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="参保人" prop="insuredName">
            <el-input
              v-model="transferQuery.insuredName"
              placeholder="请输入参保人姓名"
              clearable
              @keyup.enter.native="handleTransferQuery"
            />
          </el-form-item>
          <el-form-item label="时间范围">
            <el-date-picker
              v-model="dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              @change="handleTransferQuery"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTransferQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetTransferQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loadingTransfer" :data="transferList">
          <el-table-column label="序号" align="center" type="index" width="50" />
          <el-table-column label="转账编号" align="center" prop="transferId" />
          <el-table-column label="参保人" align="center" prop="insuredName" />
          <el-table-column label="转账时间" align="center" prop="transferTime" />
          <el-table-column label="转账金额" align="center" prop="amount" />
          <el-table-column label="转入账户类型" align="center" prop="accountType">
            <template slot-scope="scope">
              <el-tag type="primary" v-if="scope.row.accountType === 0">个人账户</el-tag>
              <el-tag type="success" v-else-if="scope.row.accountType === 1">基金账户</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作人" align="center" prop="operator" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="viewTransferDetail(scope.row)"
              >查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <pagination
          v-show="transferTotal>0"
          :total="transferTotal"
          :page.sync="transferQuery.pageNum"
          :limit.sync="transferQuery.pageSize"
          @pagination="getTransferList"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 详情对话框 -->
    <el-dialog title="报销详情" :visible.sync="detailOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="参保人">{{ detail.insuredName }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ detail.idCard }}</el-descriptions-item>
        <el-descriptions-item label="医疗机构">{{ detail.hospitalName }}</el-descriptions-item>
        <el-descriptions-item label="就诊日期">{{ detail.visitDate }}</el-descriptions-item>
        <el-descriptions-item label="批准时间">{{ detail.approveTime }}</el-descriptions-item>
        <el-descriptions-item label="报销金额">{{ detail.reimbursementAmount }} 元</el-descriptions-item>
        <el-descriptions-item label="诊断信息" :span="2">{{ detail.diagnosis }}</el-descriptions-item>
        <el-descriptions-item label="备注信息" :span="2">{{ detail.remark }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 转账对话框 -->
    <el-dialog title="处理报销转账" :visible.sync="transferOpen" width="500px" append-to-body>
      <el-form ref="transferForm" :model="transferForm" label-width="100px">
        <el-form-item label="参保人">
          <span>{{ transferForm.insuredName }}</span>
        </el-form-item>
        <el-form-item label="报销金额">
          <span>{{ transferForm.reimbursementAmount }} 元</span>
        </el-form-item>
        <el-form-item label="转入账户类型" prop="accountType" required>
          <el-radio-group v-model="transferForm.accountType">
            <el-radio label="0">个人账户</el-radio>
            <el-radio label="1">基金账户</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input
            v-model="transferForm.remark"
            type="textarea"
            placeholder="请输入备注信息"
            maxlength="200"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTransfer">确认转账</el-button>
        <el-button @click="cancelTransfer">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 转账记录详情对话框 -->
    <el-dialog title="转账记录详情" :visible.sync="transferDetailOpen" width="600px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="转账编号">{{ transferDetail.transferId }}</el-descriptions-item>
        <el-descriptions-item label="参保人">{{ transferDetail.insuredName }}</el-descriptions-item>
        <el-descriptions-item label="身份证号">{{ transferDetail.idCard }}</el-descriptions-item>
        <el-descriptions-item label="转账时间">{{ transferDetail.transferTime }}</el-descriptions-item>
        <el-descriptions-item label="转账金额">{{ transferDetail.amount }} 元</el-descriptions-item>
        <el-descriptions-item label="转入账户类型">
          <el-tag type="primary" v-if="transferDetail.accountType === 0">个人账户</el-tag>
          <el-tag type="success" v-else-if="transferDetail.accountType === 1">基金账户</el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="操作人">{{ transferDetail.operator }}</el-descriptions-item>
        <el-descriptions-item label="医疗机构">{{ transferDetail.hospitalName }}</el-descriptions-item>
        <el-descriptions-item label="转出账户">医保局统筹账户</el-descriptions-item>
        <el-descriptions-item label="备注信息" :span="2">{{ transferDetail.remark }}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="transferDetailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listReimbursement, transferRecord, transferMoney} from "@/api/medical/fundbank";
export default {
  name: "FundBank",
  data() {
    return {
      // 当前激活的选项卡
      activeTab: 'reimbursement',
      // 遮罩层
      loading: false,
      loadingTransfer: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      transferTotal: 0,
      // 报销申请列表
      reimbursementList: [],
      // 转账记录列表
      transferList: [],
      // 日期范围
      dateRange: [],
      // 详情对话框
      detailOpen: false,
      // 转账对话框
      transferOpen: false,
      // 转账详情对话框
      transferDetailOpen: false,
      // 详情数据
      detail: {},
      // 转账详情
      transferDetail: {},
      // 转账表单
      transferForm: {
        id: null,
        insuredName: '',
        reimbursementAmount: '',
        accountType: '1',
        remark: ''
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        insuredName: undefined,
        status: undefined
      },
      // 转账查询参数
      transferQuery: {
        pageNum: 1,
        pageSize: 10,
        insuredName: undefined
      }
    };
  },
  created() {
    this.getList();
    this.getTransferList();
  },
  methods: {
    /** 查询报销申请列表 */
    async getList() {
      this.loading = true;
      const res = await listReimbursement();
      this.reimbursementList = res.rows.map(item => ({
        id: item.id,
        insuredName: item.insuredPersons.name,
        idCard: item.idCard,
        hospitalName: item.medicalInstitution,
        visitDate: item.insuranceDate || '-', 
        approveTime: item.approvalTime,
        reimbursementAmount: item.reimbursementAmount,
        status: item.status, 
        diagnosis: '-', 
        remark: '-'
      })) || [];
      
      this.loading = false;
    },
    /** 查询转账记录 */
    async getTransferList() {
      this.loadingTransfer = true;
      const res = await transferRecord();
      
      // 填充转账记录列表
      this.transferList = res.rows.map(item => ({
        transferId: item.transferNumber,
        insuredName: item.insuredPersons.name,
        idCard: item.insuredPersons.idCard,
        transferTime: item.transferTime,
        amount: item.transferAmount.toFixed(2), 
        accountType: item.accountType, 
        operator: item.operator,
        hospitalName: '-', // 假设这里用保险类型代替医院名称
        remark: '-'
      })) || [];
      
      this.transferTotal = this.transferList.length; // 更新总数
      this.loadingTransfer = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 搜索转账记录按钮操作 */
    handleTransferQuery() {
      this.transferQuery.pageNum = 1;
      this.getTransferList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 重置转账查询按钮操作 */
    resetTransferQuery() {
      this.resetForm("transferForm");
      this.dateRange = [];
      this.handleTransferQuery();
    },
    /** 查看详情按钮操作 */
    handleDetail(row) {
      this.detail = row;
      this.detailOpen = true;
    },
    /** 处理转账按钮操作 */
    handleTransfer(row) {
      this.transferForm = {
        id: row.id,
        insuredName: row.insuredName,
        reimbursementAmount: row.reimbursementAmount,
        accountType: '1',
        remark: ''
      };
      this.transferOpen = true;
    },
    /** 取消转账 */
    cancelTransfer() {
      this.transferOpen = false;
      this.resetForm("transferForm");
    },
    /** 提交转账 */
   async submitTransfer() {
      this.$confirm('确认向参保人 ' + this.transferForm.insuredName + ' 转账 ' + this.transferForm.reimbursementAmount + ' 元吗?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(async () => {
        this.loading = true;
        await transferMoney({
          id: this.transferForm.id,
          reimbursementAmount: this.transferForm.reimbursementAmount,
          accountType: this.transferForm.accountType,
          remark: this.transferForm.remark
        })
        // 关闭对话框
        this.transferOpen = false;
        this.$message({
          type: "success",
          message: "转账成功，已生成转账记录"
        });
        this.loading = false;
        this.getList();
        this.getTransferList();
      }).catch(() => {});
    },
    /** 查看转账详情 */
    viewTransferDetail(row) {
      this.transferDetail = row;
      this.transferDetailOpen = true;
    }
  }
};
</script>

<style scoped>
.el-card {
  margin-bottom: 20px;
}
</style> 